//
//  BHNetWorkRequest.m
//  PropertyMananger
//
//  Created by bh on 16/12/20.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "BHNetWorkRequest.h"
#import "MBProgressHUD+YC.h"

@implementation BHNetWorkRequest

#pragma mark - GET
+(void)startGetWithUrl:(NSString *)urlStr Success:(SuccessBlock)successBlock Fail:(FailBlock)failBlock param:(NSDictionary *)param{
    //检测网络
    [BHCommonAppDelegate isConnectionAvailable];
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    [manager GET:urlStr parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        successBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failBlock(error.localizedDescription);
        
        if([error.localizedDescription isEqualToString:@"The request timed out."]){
            [MBProgressHUD hideHUD];

            [MBProgressHUD showError:@"网络请求超时" time:1.5 ];
        }else if ([error.localizedDescription isEqualToString:@"The Internet connection appears to be offline."]){
            //            [MBProgressHUD hideHUD];
            //            [MBProgressHUD showError:@"网络链接失败，请检查网络" time:1.5];
        }else if ([error.localizedDescription isEqualToString:@"Could not connect to the server."]){
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"无法链接服务器" time:1.5];
        }else{
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:error.localizedDescription time:1.5];
        }
    }];
}

#pragma mark - POST无请求体
+(void)startPostWithUrl:(NSString *)urlStr Success:(SuccessBlock)successBlock Fail:(FailBlock)failBlock param:(NSDictionary *)param{
    //检测网络
    [BHCommonAppDelegate isConnectionAvailable];
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    [manager POST:urlStr parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUD];
        successBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        failBlock(error.localizedDescription);
        if([error.localizedDescription isEqualToString:@"The request timed out."]){
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"网络请求超时" time:1.5];
        }else if ([error.localizedDescription isEqualToString:@"The Internet connection appears to be offline."]){
            //            [MBProgressHUD hideHUD];
            //            [MBProgressHUD showError:@"网络链接失败，请检查网络" time:1.5];
        }else if ([error.localizedDescription isEqualToString:@"Could not connect to the server."]){
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"无法链接服务器" time:1.5];
        }else{
            [MBProgressHUD showError:error.localizedDescription time:1.5];
        }
        NSLog(@"%@",error.localizedDescription);
    }];
}


#pragma mark -  POST函请求体
+(void)postBodyWithUrl:(NSString *)urlStr Success:(SuccessBlock)successBlock Fail:(FailBlock)failBlock Body:(BodyBlock)bodyBlock param:(NSDictionary *)param{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager POST:urlStr parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        bodyBlock(formData);
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUD];
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if([error.localizedDescription isEqualToString:@"The request timed out."]){
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"网络请求超时" time:1.5];
        }else if ([error.localizedDescription isEqualToString:@"The Internet connection appears to be offline."]){
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"网络链接失败，请检查网络" time:1.5];
        }else if ([error.localizedDescription isEqualToString:@"Could not connect to the server."]){
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"无法链接服务器" time:1.5];
        }else{
            [MBProgressHUD showError:error.localizedDescription time:1.5];
        }
        
    }];
    
}


@end
